package com.amigo.online.provider.user.controller;


import com.amigo.online.common.param.result.ProbePersonListInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.amigo.online.common.base.controller.BaseController;
import com.amigo.online.common.core.response.Response;
import com.amigo.online.common.core.response.StatusCode;
import com.amigo.online.common.param.dto.adv.AdvTantanDto;
import com.amigo.online.common.param.dto.user.ProbePeopleDto;
import com.amigo.online.common.util.CommonValidator;
import com.amigo.online.provider.user.exception.UserException;
import com.amigo.online.provider.user.service.TanService;
import com.amigo.online.provider.user.service.UserLikeService;

/**
 * 探探相关
 *
 * @author amigo
 */
@RestController
@RequestMapping(value = "/tan", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class TanController extends BaseController {

    @Autowired
    private TanService tanService;

    @Autowired
    private UserLikeService userLikeService;

    @GetMapping(value = "/openT")
    public ResponseEntity<Response> openTanTan() {
        return success(tanService.openT());
    }

    @GetMapping(value = "/closeT")
    public ResponseEntity<Response> closeTanTan() {
        return success(tanService.closeT());
    }

    @GetMapping(value = "/like/{userId}")
    public ResponseEntity<Response> likeOne(@PathVariable Long userId) {
        return success(userLikeService.likeOne(userId));
    }

    @GetMapping(value = "/dislike/{userId}")
    public ResponseEntity<Response> dislikeOne(@PathVariable Long userId) {
        boolean is = userLikeService.dislikeOne(userId);
        return success(is);
    }

    /**
     * 探探快速匹配(快速配对)
     *
     * @param
     * @return
     */
    @PostMapping(value = "/probe")
    public ResponseEntity<Response> probePeople(@RequestBody ProbePeopleDto probePeopleDto) {
        logger.info("经度:" + probePeopleDto.getLng() + "纬度：" + probePeopleDto.getLat());
        if (probePeopleDto.getLat() == null) {
            throw new UserException(StatusCode.BAD_REQUEST);
        }
        if (probePeopleDto.getLng() == null) {
            throw new UserException(StatusCode.BAD_REQUEST);
        }
        if (!CommonValidator.validate(probePeopleDto)) {
            throw new UserException(StatusCode.BAD_REQUEST);
        }
        Double[] point = new Double[2];
        point[0] = Double.parseDouble(probePeopleDto.getLng());
        point[1] = Double.parseDouble(probePeopleDto.getLat());

        AdvTantanDto advTantanDto = new AdvTantanDto(point[0], point[1], probePeopleDto.getCity(),
                probePeopleDto.getLanguage(), probePeopleDto.getHeight(), probePeopleDto.getWidth());
        ProbePersonListInfo probePersonListInfo = userLikeService.probePeople(probePeopleDto, advTantanDto);
        return success(probePersonListInfo);
    }

}
